package cart

import (
	"context"
	"gitee.com/enjoy_5/cart-rpc/internal/server"
	proto "gitee.com/enjoy_5/cart-rpc/proto"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type CartSever struct {
	proto.UnimplementedCartServer
}

// 添加购物车
func (c *CartSever) CreateCart(ctx context.Context, in *proto.CreateCartRequest) (*proto.CreateCartResponse, error) {
	if in.GetIn().GoodsId == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "goods id cannot be 0")
	}
	if in.GetIn().Quantity == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "quantity must be greater than 0")
	}
	if in.GetIn().UserId == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "user id cannot be 0")
	}
	if in.GetIn().SkuId == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "sku id cannot be 0")
	}

	createCart, err := server.CreateCart(in.GetIn())
	if err != nil {
		return nil, status.Errorf(codes.Internal, "create cart failed")
	}

	return &proto.CreateCartResponse{Info: createCart}, nil
}

// 删除购物车(根据需求删除)
func (c *CartSever) DeleteCart(ctx context.Context, in *proto.DeleteCartRequest) (*proto.DeleteCartResponse, error) {
	if in.GoodsId == nil {
		return nil, status.Errorf(codes.InvalidArgument, "goods id cannot be 0")
	}
	if in.UserId == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "user id cannot be 0")
	}

	_, err := server.DeleteCart(in.GoodsId, int(in.UserId))
	if err != nil {
		return nil, status.Errorf(codes.Internal, "delete cart failed")
	}

	return &proto.DeleteCartResponse{}, nil
}

// 修改购物车商品数量
func (c *CartSever) UpdateCartGoodsNumber(ctx context.Context, in *proto.UpdateCartGoodsNumberRequest) (*proto.UpdateCartGoodsNumberResponse, error) {
	if in.GoodsId == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "goods id cannot be 0")
	}
	if in.UserId == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "user id cannot be 0")
	}

	number, err := server.UpdateCartGoodsNumber(in.GoodsId, in.UserId, in.Quantity)
	if err != nil {
		return nil, status.Errorf(codes.Internal, "update cart goods number failed")
	}

	return &proto.UpdateCartGoodsNumberResponse{Info: number}, nil
}

// 购物车列表
func (c *CartSever) ListCart(ctx context.Context, in *proto.ListCartRequest) (*proto.ListCartResponse, error) {
	if in.UserId == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "user id cannot be 0")
	}

	cart, err := server.ListCart(in.UserId)
	if err != nil {
		return nil, status.Errorf(codes.Internal, "get cart list failed")
	}

	return &proto.ListCartResponse{List: cart}, nil
}

// 查询购物车详情
func (c *CartSever) GetCartInfo(ctx context.Context, in *proto.GetCartInfoRequest) (*proto.GetCartInfoResponse, error) {
	if in.UserId == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "user id cannot be 0")
	}
	if in.GoodsId == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "goods id cannot be 0")
	}

	cart, err := server.GetCartInfo(in.UserId, in.GoodsId)
	if err != nil {
		return nil, status.Errorf(codes.Internal, "get cart info failed")
	}

	return &proto.GetCartInfoResponse{Info: cart}, nil
}
